﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    [CustomEditor(typeof(Shader))]
    internal class ShaderInspector : Editor
    {
        private static string[] kPropertyTypes = new string[] { "Color: ", "Vector: ", "Float: ", "Range: ", "Texture: " };
        private static string[] kShaderLevels = new string[] { "Fixed function", "SM1.x", "SM2.0", "SM3.0", "SM4.0", "SM5.0" };
        private static string[] kTextureTypes = new string[] { "No Texture?: ", "1D texture: ", "Texture: ", "Volume: ", "Cubemap: ", "Any texture: " };
        internal static Styles ms_Styles;

        private static string GetPropertyType(Shader s, int index)
        {
            ShaderUtil.ShaderPropertyType propertyType = ShaderUtil.GetPropertyType(s, index);
            if (propertyType == ShaderUtil.ShaderPropertyType.TexEnv)
            {
                return kTextureTypes[(int) ShaderUtil.GetTexDim(s, index)];
            }
            return kPropertyTypes[(int) propertyType];
        }

        public override void OnInspectorGUI()
        {
            int errorCount;
            if (ms_Styles == null)
            {
                ms_Styles = new Styles();
            }
            GUI.enabled = true;
            Shader target = base.target as Shader;
            EditorGUI.indentLevel = 0;
            if ((target == null) || !target.isSupported)
            {
                GUILayout.Label("Shader has errors or is not supported by your graphics card", EditorStyles.helpBox, new GUILayoutOption[0]);
            }
            else
            {
                EditorGUILayout.LabelField("Cast shadows", (!ShaderUtil.HasShadowCasterPass(target) || !ShaderUtil.HasShadowCollectorPass(target)) ? "no" : "yes", new GUILayoutOption[0]);
                EditorGUILayout.LabelField("Render queue", ShaderUtil.GetRenderQueue(target).ToString(), new GUILayoutOption[0]);
                EditorGUILayout.LabelField("LOD", ShaderUtil.GetLOD(target).ToString(), new GUILayoutOption[0]);
                EditorGUILayout.LabelField("Geometry", ShaderUtil.GetSourceChannels(target), new GUILayoutOption[0]);
                EditorGUILayout.LabelField("Vertex shader", kShaderLevels[(int) ShaderUtil.GetVertexModel(target)], new GUILayoutOption[0]);
                EditorGUILayout.LabelField("Fragment shader", kShaderLevels[(int) ShaderUtil.GetFragmentModel(target)], new GUILayoutOption[0]);
                EditorGUILayout.LabelField("Ignore projector", !ShaderUtil.DoesIgnoreProjector(target) ? "no" : "yes", new GUILayoutOption[0]);
                GUILayout.Label("Properties:", EditorStyles.boldLabel, new GUILayoutOption[0]);
                errorCount = ShaderUtil.GetPropertyCount(target);
                for (int j = 0; j < errorCount; j++)
                {
                    string propertyName = ShaderUtil.GetPropertyName(target, j);
                    string str2 = GetPropertyType(target, j) + ShaderUtil.GetPropertyDescription(target, j);
                    EditorGUILayout.LabelField(propertyName, str2, new GUILayoutOption[0]);
                }
            }
            errorCount = ShaderUtil.GetErrorCount(target);
            if (errorCount != 0)
            {
                GUILayout.Label("Errors:", EditorStyles.boldLabel, new GUILayoutOption[0]);
            }
            EditorGUIUtility.SetIconSize(new Vector2(16f, 16f));
            for (int i = 0; i < errorCount; i++)
            {
                string shaderErrorMessage = ShaderUtil.GetShaderErrorMessage(target, i);
                bool shaderErrorWarning = ShaderUtil.GetShaderErrorWarning(target, i);
                int shaderErrorLine = ShaderUtil.GetShaderErrorLine(target, i);
                if (shaderErrorLine >= 0)
                {
                    shaderErrorMessage = shaderErrorMessage + " at line " + shaderErrorLine;
                }
                GUILayout.Label(new GUIContent(shaderErrorMessage, !shaderErrorWarning ? ms_Styles.errorIcon : ms_Styles.warningIcon), new GUILayoutOption[0]);
            }
            EditorGUIUtility.SetIconSize(Vector2.zero);
            EditorGUILayout.BeginHorizontal(new GUILayoutOption[0]);
            EditorGUILayout.Space();
            if (GUILayout.Button("Open compiled shader", "MiniButton", new GUILayoutOption[0]))
            {
                ShaderUtil.OpenCompiledShader(target);
                GUIUtility.ExitGUI();
            }
            EditorGUILayout.EndHorizontal();
            EditorGUIUtility.LookLikeInspector();
        }

        internal class Styles
        {
            public Texture2D errorIcon = EditorGUIUtility.LoadIcon("console.erroricon.sml");
            public Texture2D warningIcon = EditorGUIUtility.LoadIcon("console.warnicon.sml");
        }
    }
}

